﻿using Nest;
using System;

namespace Infrastructure.ElasticSearch.ParentChilds
{
    public class HighlightBaseSpecification<T> : ISearchSpecification<ISearchRequest<T>> where T : class
    {
        private readonly ISearchSpecification<ISearchRequest<T>> _query;

        public HighlightBaseSpecification(ISearchSpecification<ISearchRequest<T>> query)
            => _query = query ?? throw new ArgumentNullException($"{nameof(query)} cannot be null.");

        public ISearchRequest<T> IsSatisfiedBy()
            => new SearchDescriptor<T>()
                .Query(q => _query
                    .IsSatisfiedBy()
                    .Query
                )
                .Highlight(h => h
                    .PreTags("<strong>")
                    .PostTags("</strong>")
                    .FragmentSize(150)
                    .NumberOfFragments(2)
                    .Fields(f => f
                        .Field("*")
                        .ForceSource()
                    )
                );
    }
}
